<?php
	/**
	 * @author		: Mauricio Amorim
	 * @date		: 10/11/2010
	 * @version		: 1.0
	 * @desc		: tabela contact 
	 * 				 
			`id`, 
			`active`, 
			`subject`, 
			`name`, 
			`company`, 
			`email`, 
			`phone`, 
			`message`, 
			`date`
	 */

	include_once "library/".Config::FOLDER_APPLICATION."/modules/contact/vo/ContactVO.class.php";
	include_once "library/facil3/core/DbInterface.class.php";
	include_once "library/facil3/interface/dao/PatternDAO.class.php";
	
	class ContactDAO extends DbInterface implements PatternDAO{

		public static $instance;
		public static function getInstance(){
			if(!self::$instance){
				self::$instance = new ContactDAO();
			}
			return self::$instance;
		}
		
		
		public function __construct(){
			parent::__construct();
		}
		public function insertVO($VO){
			return $this->insert( 
						$VO->active, 
						$VO->name,
						$VO->subject,
						$VO->company,
						$VO->email,
						$VO->phone,
						$VO->message,
						$VO->date
			);
		}


		/**
		 * @param $active
		 * @param $name
		 * @param $subject
		 * @param $company
		 * @param $email
		 * @param $phone
		 * @param $message
		 * @param $date
		 * @return ReturnDataVO
		 */
		public function insert(
						$active = 0, 
						$name = NULL,
						$subject = NULL,
						$company = NULL,
						$email = NULL,
						$phone = NULL,
						$message = NULL
		  			){
			
			$query = "INSERT INTO contact (
						 `id`, 
						 `active`, 
						 `name`, 
						 `subject`, 
						 `company`, 
						 `email`, 
						 `phone`, 
						 `message`, 
					 	 `date`
						)VALUES(
								NULL,
								1,
								'".DataHandler::forceString($name)."',
								'".DataHandler::forceString($subject)."',
								'".DataHandler::forceString($company)."',
								'".DataHandler::forceString($email)."',
								'".DataHandler::forceString($phone)."',
								'".DataHandler::forceString($message)."',
								NOW()
						)";
			$ReturnDataVO = parent::query($query, TRUE);
			//Debug::print_r($ReturnDataVO);
			return $ReturnDataVO;
		}
		/**
	     * @param $id number
	     * @return class ReturnDataVO 
	     */
		public function active($id){
			$query = "UPDATE contact SET `active` = 1 WHERE id = '".DataHandler::forceInt($id)."' LIMIT 1 ";
			$ReturnDataVO = parent::query($query);
			//Debug::print_r($ReturnDataVO);
			return $ReturnDataVO;
		}
		
		/* @$id number
		 * @return class ReturnDataVO 
		 */
		public function deactive($id){
			$query = "UPDATE contact SET `active` = 0 WHERE id = '".DataHandler::forceInt($id)."' LIMIT 1 ";
			$ReturnDataVO = parent::query($query);
			//Debug::print_r($ReturnDataVO);
			return $ReturnDataVO;
		}
		/**
	     * @param $id number
	     * @return class ReturnDataVO 
	     */
		public function delete($id){
			$query = "UPDATE contact SET `active` = 0 WHERE id = '".DataHandler::forceInt($id)."' LIMIT 1 ";
			$ReturnDataVO = parent::query($query);
			//Debug::print_r($ReturnDataVO);
			return $ReturnDataVO;
		}
		/**
	     * @param VO
	     * @return class ReturnDataVO 
	     */
		public function updateVO($VO){
			return $this->update(
						$VO->id, 
						$VO->active, 
						$VO->name,
						$VO->subject,
						$VO->company,
						$VO->email,
						$VO->phone,
						$VO->message,
						$VO->date
						
				);
		}
		
		/**
		 * @param $id
		 * @param $active
		 * @param $name
		 * @param $subject
		 * @param $company
		 * @param $email
		 * @param $phone
		 * @param $message
		 * @return ReturnDataVO
		 */
		public function update(
							$id,
							$active = 0, 
							$name = NULL,
							$subject = NULL,
							$company = NULL,
							$email = NULL,
							$phone = NULL,
							$message = NULL
  						){
 
  			$ReturnDataVO = new ReturnDataVO();
			$query = "UPDATE contact SET ";
			if($id == NULL && $id <= 0){
				$ReturnDataVO->success = FALSE;
				$ReturnDataVO->code_return = DbInterface::ERROR_UPDATE_DONOT_HAVE_ID;
				return $ReturnDataVO;
			}
			$comma = "";
			if($active !== NULL){
				$query .= $comma." `active` = '".DataHandler::forceInt($active)."'";
				$comma = ", ";
			}
			if($name !== NULL){
				$query .= $comma." `name` = '".DataHandler::forceString($name)."'";
				$comma = ", ";
			}
			if($subject !== NULL){
				$query .= $comma." `subject` = '".DataHandler::forceString($subject)."'";
				$comma = ", ";
			}
			if($company !== NULL){
				$query .= $comma." `company` = '".DataHandler::forceString($company)."'";
				$comma = ", ";
			}
			if($email !== NULL){
				$query .= $comma." `email` = '".DataHandler::forceString($email)."'";
				$comma = ", ";
			}
			if($phone !== NULL){
				$query .= $comma." `phone` = '".DataHandler::forceString($phone)."'";
				$comma = ", ";
			}
			if($message !== NULL){
				$query .= $comma." `message` = '".DataHandler::forceString($message)."'";
				$comma = ", ";
			}
			$query .= "	WHERE id = '".DataHandler::forceInt($id)."' LIMIT 1";
			if($comma == ""){
				$ReturnDataVO->success = FALSE;
				$ReturnDataVO->code_return = DbInterface::ERROR_UPDATE_DONOT_HAVE_UPDATES;
				//Debug::print_r($ReturnDataVO);
				return $ReturnDataVO;
			}else{
				$ReturnDataVO = parent::query($query);
				//Debug::print_r($ReturnDataVO);
				return $ReturnDataVO;
			}
		}
		/**
	     * @param $id number
	     * @return class ReturnDataVO 
	     */
		public function selectById($id){
			$ReturnDataVO = $this->select(DbInterface::RETURN_STD_OBJECT, $id);
			return $ReturnDataVO;
		}
		
		/**
		 * @param $typeOfReturn
		 * @param $id
		 * @param $active
		 * @param $name
		 * @param $subject
		 * @param $company
		 * @param $email
		 * @param $phone
		 * @param $message
		 * @param $date
		 * @param $date_symbol
		 * @param $quant_limit
		 * @param $quant_started
		 * @return ReturnDataVO
		 */
		public function select(
							$typeOfReturn = NULL, 
							$id = NULL, 
							$active = NULL, 
							$name = NULL,
							$subject = NULL,
							$company = NULL,
							$email = NULL,
							$phone = NULL,
							$message = NULL,
							$date = NULL,
							$date_symbol = ">=",
							$quant_limit = NULL, 
							$quant_started = NULL
						){

			$arrayFilter = array();
			$query = "SELECT 
							* 
						FROM 
					  		contact 
					  WHERE 1 ";
			$this->addFilter(
								$query,
								$id, 
								$active, 
								$name,
								$subject,
								$company,
								$email,
								$phone,
								$message,
								$date,
								$date_symbol 
							);
			
			$queryCount = "Select 
								COUNT(*) AS count
							FROM 
								contact
							WHERE 1
  	  					";
			$this->addFilter(
								$queryCount,
								$id, 
								$active, 
								$name,
								$subject,
								$company,
								$email,
								$phone,
								$message,
								$date,
								$date_symbol 
							);
			
			//limite
			$query .= parent::limit($quant_limit, $quant_started);
			//executa a query
			$ReturnDataVO = parent::query($query);
			//echo $query;
			//executa a query para gerar count total
			$ReturnTotalDataVO = parent::query($queryCount);
			
			if($ReturnDataVO->success){
				switch($typeOfReturn){
					case DbInterface::RETURN_ARRAY:
						$ReturnDataVO->fetchAll(ReturnDataVO::TYPE_FETCH_ARRAY);
						break;
					case DbInterface::RETURN_VO:
						$ReturnDataVO->fetchAllVO($this);
						break;
					case DbInterface::RETURN_STD_OBJECT:
					default:
						//retornar tudo em objeto
						$ReturnDataVO->fetchAll(ReturnDataVO::TYPE_FETCH_OBJECT);
						break;
				}
				$ReturnTotalDataVO->fetchAll(ReturnDataVO::TYPE_FETCH_OBJECT);
				$tempTotal = $ReturnTotalDataVO->result[0]->count;
				$ReturnDataVO->count_total = $tempTotal;
				
				//echo Debug::print_r($ReturnDataVO);
				return $ReturnDataVO;
			} else {
				return $ReturnDataVO;
			}
						
		}

		
		private function addFilter(
								&$query,
								$id, 
								$active, 
								$name,
								$subject,
								$company,
								$email,
								$phone,
								$message,
								$date,
								$date_symbol 
							){

			if($id !== NULL){
				$query .= " AND id = '".DataHandler::forceInt($id)."'";
			}
			if($active !== NULL){
				$query .= " AND `active` = '".DataHandler::forceInt($active)."'";
			}
			if($name !== NULL){
				$query .= " AND `name` = '".DataHandler::forceString($name)."'";
			}
			if($subject !== NULL){
				$query .= " AND `subject` = '".DataHandler::forceString($subject)."'";
			}
			if($company !== NULL){
				$query .= " AND `company` = '".DataHandler::forceString($company)."'";
			}
			if($email !== NULL){
				$query .= " AND `email` = '".DataHandler::forceString($email)."'";
			}
			if($phone !== NULL){
				$query .= " AND `phone` = '".DataHandler::forceString($phone)."'";
			}
			if($message !== NULL){
				$query .= " AND `message` = '".DataHandler::forceString($message)."'";
			}
			if($date !== NULL){
				$date_symbol = parent::compareSimble($date_symbol);
				$date = parent::dateHandlerScape($date);
				$query .= " AND `date` $date_symbol $date ";
			}
		}

		public function getVO(){
			return new ContactVO();
		}
}